package com.wit.witmg.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wit.witmg.entity.TMgr;
import com.wit.witmg.mapper.TMgrMapper;
import com.wit.witmg.service.ITMgrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wit.witmg.util.ErrorCode;
import com.wit.witmg.util.JsonResult;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author bob
 * @since 2025-06-10
 */
@Service
public class TMgrServiceImpl extends ServiceImpl<TMgrMapper, TMgr> implements ITMgrService {

    @Override
    public Object add(HttpServletRequest request, String json) {
        JSONObject obj=JSONObject.parseObject(json);
        if(obj==null){
            return JsonResult.error(ErrorCode.ERR_200);
        }

        TMgr t=new TMgr();
        t.setMid(obj.getString("id"));
        t.setJson(json);
        t.setDatetime(Long.valueOf(obj.getString("datetime")));
        t.setDate(new Date());
        if(save(t)){
            return JsonResult.success();
        }
        return JsonResult.error(ErrorCode.ERR_504);
    }

    @Override
    public Object get(HttpServletRequest request, String id,Long dt0,Long dt1) {
        LambdaQueryWrapper<TMgr> qw=new LambdaQueryWrapper<>();
        qw.eq(TMgr::getMid,id);
        qw.ge(dt0!=null,TMgr::getDate,dt0);
        qw.le(dt1!=null,TMgr::getDate,dt1);
        return JsonResult.success(list(qw));
    }
}
